package org.yxing.palpalgps.adapter;

import java.util.ArrayList;
import java.util.List;

import org.yxing.palpalgps.R;
import org.yxing.palpalgps.domain.AsyncImageLoader;
import org.yxing.palpalgps.models.ListUserItem;
import org.yxing.palpalgps.utils.Helper;
import org.yxing.palpalgps.widget.AsyncImageView;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FriendListAdapter extends BaseAdapter{
    
    private List<ListUserItem> data;
    private AsyncImageLoader mAsyncImageLoader;
    
    private Context mContext;
    
    public FriendListAdapter(Context context) {
        mContext = context;
        data = new ArrayList<ListUserItem>();
        mAsyncImageLoader = new AsyncImageLoader();
    }

    public int getCount() {
        return data.size();
    }

    public ListUserItem getItem(int position) {
        return data.get(position);
    }

    public long getItemId(int position) {
        return getItem(position).getUid();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.firend_list, null);
        }
        TextView nameView = (TextView) convertView.findViewById(R.id.name);
        TextView genderView = (TextView) convertView.findViewById(R.id.gender);
        AsyncImageView faceView = (AsyncImageView) convertView.findViewById(R.id.face);
        
        ListUserItem model = getItem(position);
        
        nameView.setText(model.getName());
        genderView.setText(model.getGender().equals("m") ? mContext.getString(R.string.male) : mContext.getString(R.string.female));
        
        String face = model.getPortrait();
        faceView.setDefaultDrawable(mContext.getResources().getDrawable(R.drawable.default_avatar));
        if (!TextUtils.isEmpty(face)) {
            String url = Helper.getSmallPictureUrl(face);
            faceView.setUrl(url);
        }
            
        return convertView;
    }
    
    public void add(ListUserItem listUserItem) {
        data.add(listUserItem);
        notifyDataSetChanged();
    }
    
    public void remove(int position) {
        data.remove(position);
        notifyDataSetChanged();
    }
    
    public void clear() {
        data.clear();
    }
}
